package com.qf.ssm.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ssm.cache.CategoriesCache;
import com.qf.ssm.dto.ResultCode;
import com.qf.ssm.event.CategoryChangeEvent;
import com.qf.ssm.exceptions.AddErrorException;
import com.qf.ssm.exceptions.DeleteErrorException;
import com.qf.ssm.exceptions.UpdateErrorException;
import com.qf.ssm.mapper.CategoriesMapper;
import com.qf.ssm.pojo.Categories;
import com.qf.ssm.pojo.CheckStatus;
import com.qf.ssm.pojo.Devices;
import com.qf.ssm.pojo.User;
import com.qf.ssm.service.CategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@Service
@Transactional
public class CategoriesServiceImpl implements CategoriesService {

    private CategoriesMapper categoriesMapper;

    @Autowired
    public void setCategoriesMapper(CategoriesMapper categoriesMapper) {
        this.categoriesMapper = categoriesMapper;
    }
    @Autowired
    public ApplicationContext context;

    @Autowired
    private CategoriesCache  categoriesCache;

    @Autowired
    public CategoryChangeEvent event;

    @Override
    public void addCategories(Categories categories) {
        if (categories == null || categories.isNull(CheckStatus.ADD)) {
            throw new AddErrorException("数据为空", ResultCode.DATA_NOT_ALLOW_NUL);
        }

        try {
            categoriesMapper.addCategories(categories);
            context.publishEvent(event);//更新缓存
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddErrorException(e.getMessage(), ResultCode.DATA_NOT_ALLOW_NUL);

        }
    }

    @Override
    public PageInfo<Categories> findAllCategoriesByPage(int page, int limit, String categoryname, String status) {
       PageInfo<Categories>pageInfo=null;
        List<Categories> categoriesList = categoriesCache.getAllData();
        List<Categories> realCategoriesList  =categoriesList.stream().filter(categories -> {
            boolean isFetchName = true;
            boolean isFetchStatus = true;
            String categoryname1 = categories.getCategoryname();
            String status1 = categories.getStatus();
            if(!StringUtils.isEmpty(categoryname)){
                isFetchName = categoryname1.contains(categoryname);
            }
            if(!StringUtils.isEmpty(status) && !"-100".equals(status)){
                isFetchStatus=status.equals(status1);
            }
            return isFetchName && isFetchStatus;
        }).collect(Collectors.toList());
        List<Categories> subList = realCategoriesList.stream().skip(( page - 1) * limit).limit(limit).collect(Collectors.toList());
        pageInfo = new PageInfo<>(subList);
        pageInfo.setTotal(realCategoriesList.size());//设置总长度为符合条件的集合的长度
        return pageInfo;

    }



    @Override
    public int deleteCategoriesByIds(Long[] ids) {
        if(ids==null || ids.length==0){
            //删除时必须又id
            throw  new DeleteErrorException("删除必须传递 id", ResultCode.DATA_NOT_ALLOW_NUL);
        }
        return categoriesMapper.deleteCategoriesByIds(Arrays.asList(ids));
    }

    @Override
    public int updateCategories(Categories categories) {
        if(categories.isNull(CheckStatus.UPDATE)){
            throw new UpdateErrorException("更新的数据为空", ResultCode.DATA_NOT_ALLOW_NUL);
        }
        return categoriesMapper.updateCategories(categories);
    }

    @Override
    public List<Categories> getAll() {
        //理论上缓存中的数据是完整的,所以直接返回
        return categoriesCache.getAllData();
    }
}
